<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 2016-03-16
 * Time: 19:11
 */

namespace common\repositories\common;

use common\entities\common\MyscrmDatabaseInstanceConfigEntity;
use common\repositories\BaseRepository;

class MyscrmDatabaseInstanceConfigRepository extends BaseRepository
{
    public $entity;

    public function __construct(MyscrmDatabaseInstanceConfigEntity $entity, $config = [])
    {
        $this->entity = $entity;

        return parent::__construct($config);
    }

    /**
     * @param $id
     * @return MyscrmDatabaseInstanceConfigEntity
     */
    public function getConfigById($id)
    {
        return $this->entity->findOne($id);
    }

    /**
     * 根据实例ID获取实例信息
     * @param $instanceId
     * @return array|bool
     */
    public function getConfigByInstanceId($instanceId)
    {
        return MyscrmDatabaseInstanceConfigEntity::find()
            ->select('myscrm_database_instance_config.*')
            ->rightJoin('myscrm_database_instance_mapping', 'myscrm_database_instance_config.id = myscrm_database_instance_mapping.master_database_instance_id')
            ->where(['myscrm_database_instance_config.instance_id' => $instanceId])
            ->one();
    }

    /**
     * 根据实例ID获取实例列表（主库）
     * @param $instanceId
     * @return array
     */
    public function getConfigListByInstanceIds($instanceId)
    {
        return MyscrmDatabaseInstanceConfigEntity::find()
            ->select('myscrm_database_instance_config.*')
            ->rightJoin('myscrm_database_instance_mapping', 'myscrm_database_instance_config.id = myscrm_database_instance_mapping.master_database_instance_id')
            ->where(['instance_id' => $instanceId])
            ->all();
    }
}
